<?php

include "UserTeam.php";

class UserTeamDAO extends DataLayer {
 		
	/**
	* @desc Construtor da Classe
 	*/
	function __construct($lk){
		$this->lk = $lk;	
	}

	public function getUsersTeam($idUser=0, $idTeam=0, $page=0){
	
		$this->sql ="	SELECT 
							SQL_CALC_FOUND_ROWS
							u.id_usuario, u.login, u.senha, u.nome, u.email, u.carga_horaria, DATE_FORMAT(u.data_criacao, '%d/%m/%Y') data_criacao, DATE_FORMAT(u.data_saida, '%d/%m/%Y') data_saida, u.status, u.data
							,ug.titulo grupo, ug.id_usuario_grupo, e.nome equipe, e.id_equipe, eu.carga_horaria carga_horaria_equipe
						FROM
							usuarios u
						INNER JOIN
							equipes_usuarios eu on eu.id_usuario = u.id_usuario
						INNER JOIN
							equipes e on e.id_equipe = eu.id_equipe
						INNER JOIN
							usuarios_grupos ug on ug.id_usuario_grupo = eu.id_usuario_grupo
						WHERE
							u.status = 1 AND e.status = 1";

		if($idUser > 0)						
			$this->sql .= "  AND u.id_usuario = ".$idUser;

		if($idTeam > 0)						
			$this->sql .= "  AND e.id_equipe = ".$idTeam;
		
		$this->sql .= " ORDER BY u.nome ASC";

		if($page > 0){
			$page = (int)$page;
			$page--;
			if($page < 0)
				$page = 0;
			else
				$page *= PAGINATION;

			$this->sql .= " LIMIT ".$page.",".PAGINATION;
		}
		
		$this->result = parent::select($this->sql);
		$userList = NULL;
		
		if(count($this->result) > 0){
			$i = 0;
			foreach($this->result as $row){
				$userList[$i] = new UserTeam();
				$userList[$i]->setDadosDB($row);				

				$i++;
			}
		}
		
		return $userList;
	}

	public function save($obj){
		if($obj != NULL){
			$idUser = (int)$obj["idUser"];
			$idTeam	= (int)$obj["idTeam"];

			if($idUser > 0 && $idTeam > 0){
				$result = $this->getUsersTeam($idUser, $idTeam);

				$dbObj['carga_horaria']		= $obj['hourlyLoadTeam'];
				$dbObj['id_usuario_grupo']	= $obj['idUserGroup'];

				if(count($result) > 0){
					parent::update("equipes_usuarios",$dbObj,"id_usuario = ".$idUser." AND id_equipe = ".$idTeam);
				}else{
					$dbObj['id_usuario'] = $idUser;
					$dbObj['id_equipe']	 = $idTeam;
					parent::insert("equipes_usuarios",$dbObj);
				}

				$result = $this->lk->sql_error();

				if($result["code"] == 0){
					return true;
				}
			}
		}

		return false;
	}

	public function changeUserTeam($idUser, $idTeam, $idNewTeam){
		
		if($idUser > 0 && $idTeam > 0 && $idNewTeam > 0){
			$result = $this->getUsersTeam($idUser, $idTeam);

			if(count($result) > 0){
				$dbObj['id_equipe'] = $idNewTeam;
				parent::update("equipes_usuarios",$dbObj,"id_usuario = ".$idUser." AND id_equipe = ".$idTeam);

				$result = $this->lk->sql_error();

				if($result["code"] == 0){
					return true;
				}
			}
		}

		return false;
	}

	public function deleteUserTeam($idUser, $idTeam){
		$return = 0;

		if($idUser > 0 && $idTeam > 0){
			$return = parent::delete("equipes_usuarios","id_usuario = ".$idUser." AND id_equipe = ".$idTeam);
		}

		return $return;
	}
}

?>